/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forms;

import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import entidade.AppHelper;
import entidade.Categoria;
import entidade.MobCatController;
import entidade.Registro;
import exceptions.ReturnWsException;
import form.components.RegistroListRenderer;
import java.util.Vector;

/**
 *
 * @author ademar.victorino
 */
public class RegistrosForm extends AbstractForm {

    private List list = new List();
    private final AbstractForm retForm;

    public RegistrosForm(AbstractForm returnForm) {
        this.retForm = returnForm;
        list.setListCellRenderer(new RegistroListRenderer());
        addComponent(list);
        // list.setSmoothScrolling(true);
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        addCommand(new Command("Voltar") {

            public void actionPerformed(ActionEvent evt) {
                //WaitForm.getInstance().show();
                //new Thread(new Runnable() {
                  //  public void run() {
                        retForm.show();
                    //}
                //}).start();
            }
        });

        list.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                WaitForm.getInstance().show();
                new Thread(new Runnable() {

                    public void run() {
                        listAction();
                    }
                }).start();

            }
        });
    }

    private void listAction() {
        Registro reg = (Registro) list.getSelectedItem();
        new DetalheRegistroForm().show(reg, this);
    }

    void show(Categoria catSel) {

        setTitle("Resultados: " + catSel.nome);

        try {
            Vector regs = MobCatController.getInstance().listRegistros(catSel.id);
            for (int i = 0; i < regs.size(); i++) {
                list.addItem(regs.elementAt(i));
            }
        } catch (ReturnWsException ex) {

            ex.showError(this);

        }
        super.show();
    }

    public void show(final String word) {

        //final WaitForm wf = new WaitForm("Realizando busca...");

        Thread t = new Thread(new Runnable() {

            public void run() {

                int count = 0;
                Vector vec = null;
                try {
                    vec = MobCatController.getInstance().buscarRegistros(word, AppHelper.getInstance().cidadeSelecionada);
                } catch (ReturnWsException ex) {
                    //ex.showError(this);
                }
                for (int i = 0; i < vec.size(); i++) {
                    Registro reg = (Registro) vec.elementAt(i);
                    list.addItem(reg);
                    reg = null;
                    count++;
                }
                setTitle("Resutados encontrados: " + count);
                //wf.setVisible(false);
                show();
            }
        });
        // wf.show(t);
        t.start();
    }
}
